package com.wuliqing.android.zte.fragment;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.wuliqing.android.zte.data.ContactData;
import com.wuliqing.android.zte.myapplication.R;
import com.wuliqing.android.zte.service.ContactSaveService;

public class MyDialogFragment extends DialogFragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        View v = getActivity().getLayoutInflater().inflate(R.layout.dialog_contact_edit, null);
        final TextView name = (TextView) v.findViewById(R.id.edit_contact_name);
        final TextView number = (TextView) v.findViewById(R.id.edit_contact_number);
        final Button cancel_button = (Button) v.findViewById(R.id.cancel_menu_item);
        final Button save_button = (Button) v.findViewById(R.id.save_menu_item);
        cancel_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        save_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name_text = name.getText().toString();
                String number_text = number.getText().toString();
                if (!TextUtils.isEmpty(name_text) && !TextUtils.isEmpty(number_text)) {
                    ContactData contactData = new ContactData();
                    contactData.setContact_name(name_text);
                    contactData.setContact_number(number_text);
                    Intent intent = ContactSaveService.createSaveContactIntent(getActivity(),contactData,null,null);
                    getActivity().startService(intent);
                    dismiss();
                } else {
                    Toast.makeText(getActivity(), getActivity().getResources().
                            getString(R.string.save_contact_msg), Toast.LENGTH_SHORT).show();
                }
            }
        });
        builder.setView(v);
        final Dialog dialog = builder.create();
        setCancelable(false);
        return builder.create();
    }
}
